home *** CD-ROM | disk | FTP | other *** search
- #include <Limits.h>
- #include "Independents.h"
-
- #ifndef MIN
- #define MIN(a, b) ((a) < (b) ? (a) : (b))
- #endif
-
- static void FrameRoundBW(const RectPtr theRect, ConstPatternParam bpat, short ovalP);
-
-
- static ControlHandle gToBeHilited = nil;
-
-
- UInt32 sizeAfterPaste(TEHandle activeTE, UInt32 howMuch)
- {
- return InlineGetHandleSize(TEGetText(activeTE)) - ((*activeTE)->selEnd - (*activeTE)->selStart) + howMuch;
- }
-
-
- UInt32 FindCommonPrefix(Ptr p, UInt32 lenp, Ptr q, UInt32 lenq)
- {
- Ptr limit;
- UInt32 prefixLen = 0;
-
- limit = q + MIN(lenp, lenq);
- while (q < limit) {
- if (*p++ == *q++)
- ++prefixLen;
- else
- break;
- }
-
- return prefixLen;
- }
-
-
- #ifndef GENERATENODATA
- #define GENERATENODATA 0
- #endif
-
- Boolean HasDragManager(void)
- {
- long response;
- #if !GENERATENODATA
- static Boolean tested = false;
- static
- #endif /* !GENERATENODATA */
- Boolean result = false;
-
- #if !GENERATENODATA
- if ( !tested ) {
- tested = true;
- #endif /* !GENERATENODATA */
- if (Gestalt(gestaltDragMgrAttr, &response) == noErr) {
- result = response & (1L << gestaltDragMgrPresent);
- #if GENERATINGCFM
- if (result)
- if (InstallTrackingHandler == (void *)kUnresolvedCFragSymbolAddress)
- result = false;
- #endif
- }
- #if !GENERATENODATA
- }
- #endif /* !GENERATENODATA */
-
- return result;
- }
-
- void SetDialogItemHandleText(DialogRef d, Handle text, short itemNo)
- {
- Str255 theS;
- Rect tempRect;
- Handle item;
- UInt32 len;
- short type;
-
- len = InlineGetHandleSize(text);
- if (len <= UCHAR_MAX) { // can use safe OS call
- StrLength(theS) = len;
- BlockMoveData(*text, &theS[1], len);
- GetDialogItem(d, itemNo, &type, &item, &tempRect);
- SetDialogItemText(item, theS);
- }
- else { // must use potentially dangerous trick that works, though (-: :-)
- HLockHi(text);
- TESetText(*text, len, ((DialogPeek)d)->textH);
- HUnlock(text);
- }
- }
-
- void SetDialogItemLongText(DialogRef d, const Ptr text, UInt32 len, short itemNo)
- {
- Str255 theS;
- Rect tempRect;
- Handle item;
- short type;
-
- if (len <= UCHAR_MAX) { // can use safe OS call
- StrLength(theS) = len;
- BlockMoveData(text, &theS[1], len);
- GetDialogItem(d, itemNo, &type, &item, &tempRect);
- SetDialogItemText(item, theS);
- }
- else { // must use potentially dangerous trick that works, though (-: :-)
- TESetText(text, len, ((DialogPeek)d)->textH);
- }
- }
-
- void FlashButton(DialogPtr dlg, short item)
- {
- Rect txtBox;
- ControlHandle itH;
- long dummy;
- short typ;
-
- GetDialogItem(dlg, item, &typ, (Handle *)&itH, &txtBox);
- HiliteControl(itH, 1);
- Delay(4L, &dummy);
- HiliteControl(itH, 0);
- }
-
- void BeginFlashButton(DialogPtr dlg, short item)
- {
- Rect txtBox;
- short typ;
-
- GetDialogItem(dlg, item, &typ, (Handle *)&gToBeHilited, &txtBox);
- HiliteControl(gToBeHilited, 1);
- }
-
- void EndFlashButton(void)
- {
- if (gToBeHilited) {
- HiliteControl(gToBeHilited, 0);
- gToBeHilited = nil;
- }
- }
-
- enum {
- kIsColorPort = 0xC000,
- kButtonFrameSize = 3, /* button frame's pen size */
- kButtonFrameInset = -4 /* inset rectangle adjustment around button */
- };
-
- void OutlineButton(DialogPtr oftheDialog, short myItem)
- {
- RGBColor foreCol, backCol, savedForeCol;
- PenState curPen;
- PatPtr thePat;
- GrafPtr savePort;
- Rect txtBox;
- Handle itH;
- short typ;
-
- GetDialogItem(oftheDialog, myItem, &typ, &itH, &txtBox);
- GetPort(&savePort);
- SetGrafPortOfDialog(oftheDialog);
-
- InsetRect(&txtBox, kButtonFrameInset, kButtonFrameInset);
- typ = ((txtBox.bottom - txtBox.top) >> 1) + 2;
-
- thePat = (*(ControlHandle)itH)->contrlHilite == 0 ? &qd.black : &qd.gray;
-
- GetPenState(&curPen);
- PenNormal();
- PenSize(kButtonFrameSize, kButtonFrameSize);
- if ((((CGrafPtr)(*(ControlHandle)itH)->contrlOwner)->portVersion & kIsColorPort) == kIsColorPort) {
- if ((*(ControlHandle)itH)->contrlHilite) {
- GetForeColor(&foreCol);
- savedForeCol = foreCol;
- GetBackColor(&backCol);
- if (GetGray(GetGDevice(), &backCol, &foreCol)) {
- RGBForeColor(&foreCol);
- FrameRoundRect(&txtBox, typ, typ);
- RGBForeColor(&savedForeCol);
- }
- else
- FrameRoundBW(&txtBox, thePat, typ);
- }
- else
- FrameRoundRect(&txtBox, typ, typ);
- }
- else {
- FrameRoundBW(&txtBox, thePat, typ);
- }
-
- SetPenState(&curPen);
- SetPort(savePort);
- }
-
- void FrameRoundBW(const RectPtr theRect, ConstPatternParam bpat, short ovalP)
- {
- PenPat(bpat);
- FrameRoundRect(theRect, ovalP, ovalP);
- }
-
- void DoMovableModelessUpdate(DialogRef dPtr, short defaultItem)
- {
- UpdateDialog(dPtr, dPtr->visRgn);
- if (defaultItem)
- OutlineButton(dPtr, defaultItem);
- }
-
-
- // GetPtrIndHString accepts a Handle to a STR# resource and an index;
- // it returns a pointer to the pascal string at that index.
- // It does not move memory.
- // If you passed an unlocked handle, the pointer is valid as long
- // as you don't move memory.
- // Useful when you need only read the string.
-
- StringPtr GetPtrIndHString(Handle resH, unsigned short index)
- {
- unsigned short *compPtr = (unsigned short *)*resH;
- unsigned char *spanPtr;
- unsigned short i = index;
-
- if (*compPtr++ <= i)
- spanPtr = nil;
- else {
- spanPtr = (unsigned char *)compPtr;
- while (i) {
- spanPtr += *spanPtr++;
- i--;
- }
- }
-
- return (StringPtr)spanPtr;
- }
-
-
- // GetIndHString accepts a Handle to a STR# resource and an index;
- // it copies the pascal string into dest.
- // It does not move memory. Thus, it's OK to pass an unlocked handle.
- // Useful when you need to modify the string subsequently.
-
- void GetIndHString(StringPtr dest, Handle resH, unsigned short index)
- {
- unsigned short *compPtr = (unsigned short *)*resH;
- unsigned char *spanPtr;
- unsigned short i = index;
-
- if (*compPtr++ <= i)
- StrLength(dest) = 0;
- else {
- spanPtr = (unsigned char *)compPtr;
- while (i) {
- spanPtr += *spanPtr++;
- i--;
- }
- BlockMoveData(spanPtr, dest, *spanPtr + 1L);
- }
-
- return;
- }
- /*
- short KeyState(unsigned short k )
- // k = any keyboard scan code, 0-127
- {
- KeyMap km;
-
- GetKeys(km);
- return ( ( *((unsigned char *)km + (k>>3)) >> (k & 7) ) & 1);
- }
- */
-
- Boolean ModifiersState(EventModifiers mask)
- {
- EventRecord ev;
-
- (void) EventAvail(0, &ev);
- return (ev.modifiers & mask) != 0;
- }
-
- OSType Str2OSType(ConstStr255Param theStr)
- {
- OSType result;
- Ptr source, dest;
- unsigned char i = StrLength(theStr);
-
- source = (Ptr)theStr + 1;
- dest = (Ptr)&result;
- *dest++ = *source++;
- *dest++ = *source++;
- *dest++ = *source++;
- *dest++ = *source++;
-
- if (i < 4)
- while (i < 4) {
- *--dest = 0x20;
- i++;
- }
-
- return result;
- }
-
- void OSType2Str(OSType typ, StringPtr theStr)
- {
- register StringPtr tempPtr = theStr;
- register StringPtr sourcePtr = (StringPtr)&typ;
-
- *tempPtr++ = 4;
- *tempPtr++ = *sourcePtr++;
- *tempPtr++ = *sourcePtr++;
- *tempPtr++ = *sourcePtr++;
- *tempPtr = *sourcePtr;
- }
-
- /* this is taken from "Macintosh Programming Secrets"
- by Scott Knaster & Keith Rollin;
- 96/09/17 FO: improved according to TN TE 23: International Canceling
- optimizations by Fabrizio Oddone */
-
- Boolean CmdPeriod(const EventRecord * const theEvent)
- {
- #define kModifiersMask (0xFF00 & ~cmdKey)
-
- Handle hKCHR = nil;
- Ptr KCHRPtr;
- UInt32 state;
- UInt32 keyInfo;
- UInt16 keyCode;
- Boolean result = false;
-
- if (theEvent->what == keyDown || theEvent->what == autoKey) {
- if (theEvent->modifiers & cmdKey) {
- keyCode = (theEvent->modifiers & kModifiersMask) | ((unsigned short)theEvent->message >> 8);
- state = 0;
- KCHRPtr = (Ptr) GetScriptManagerVariable(smKCHRCache);
- if (KCHRPtr == nil) {
- hKCHR = GetResource('KCHR', GetScriptVariable(GetScriptManagerVariable(smKeyScript), smScriptKeys));
- KCHRPtr = *hKCHR;
- }
-
- keyInfo = KCHRPtr ? KeyTranslate(KCHRPtr, keyCode, &state) : theEvent->message;
-
- if (hKCHR)
- ReleaseResource(hKCHR);
-
- if (((char)keyInfo == '.') || ((char)(keyInfo >> 16) == '.'))
- result = true;
- }
- }
-
- return result;
- }
-
- Handle FabGetFullPath(const FSSpecPtr fss, Boolean AUXpresent)
- {
- CInfoPBRec mypb;
- Str63 dirName;
- Handle fullPath;
- //UInt32 hCurSize;
- OSErr err = noErr;
-
- fullPath = NewHandle(StrLength(fss->name));
- if (fullPath) {
- BlockMoveData(fss->name + 1, *fullPath, StrLength(fss->name));
- if (fss->parID != fsRtParID) {
- mypb.dirInfo.ioNamePtr = dirName;
- mypb.dirInfo.ioVRefNum = fss->vRefNum;
- mypb.dirInfo.ioDrParID = fss->parID;
- mypb.dirInfo.ioFDirIndex = -1;
- do {
- mypb.dirInfo.ioDrDirID = mypb.dirInfo.ioDrParID;
- err = PBGetCatInfoSync(&mypb);
- if (err)
- break;
- else {
- if (AUXpresent) {
- if (dirName[1] != '/') {
- StrLength(dirName) += 1;
- dirName[StrLength(dirName)] = '/';
- }
- }
- else {
- StrLength(dirName) += 1;
- dirName[StrLength(dirName)] = ':';
- }
- /* hCurSize = InlineGetHandleSize(fullPath);
- SetHandleSize(fullPath, hCurSize + StrLength(dirName));
- */
- /* Add directory name to beginning of fullPath */
- (void) Munger(fullPath, 0, NULL, 0, &dirName[1], StrLength(dirName));
- err = MemError();
- if (err)
- break;
- /* else {
- BlockMoveData(*fullPath, *fullPath + StrLength(dirName), hCurSize);
- BlockMoveData(dirName + 1, *fullPath, StrLength(dirName));
- }
- */ }
- }
- while (mypb.dirInfo.ioDrDirID != fsRtDirID);
- }
- }
- if (err) {
- DisposeHandle(fullPath);
- fullPath = nil;
- }
- return fullPath;
- }
-
- void fabc2pstr(unsigned char *cs, StringPtr destps)
- {
- unsigned char *scanPtr = cs;
- unsigned char *destPtr = destps + 1;
- unsigned int count = 0;
-
- while ((*destPtr++ = *scanPtr++) && count < UCHAR_MAX)
- count++;
- StrLength(destps) = count;
- }
-
- Boolean IsOnScreen(const Rect * const r)
- {
- Point topRight;
-
- topRight.h = r->right;
- topRight.v = r->top;
- return (PtInRgn(topLeft(*r), GetGrayRgn()) || PtInRgn(topRight, GetGrayRgn()));
- }
-
- Boolean IsOnScreenWeak(Point pt)
- {
- Rect box;
-
- topLeft(box) = pt;
- box.bottom = box.top + qd.thePort->portRect.bottom - qd.thePort->portRect.top;
- box.right = box.left + qd.thePort->portRect.right - qd.thePort->portRect.left;
- return IsOnScreen(&box);
- }
-
- #pragma segment Init
-
- void SetupStringFromPrefs(StringHandle *gString, short strID)
- {
- register Handle tempH;
-
- tempH = (Handle)GetString(strID);
- if (tempH) {
- DetachResource(tempH);
- if (*gString)
- DisposeHandle((Handle)*gString);
- *gString = (StringHandle)tempH;
- }
- }
-
- UInt32 SetupULongFromPrefs(short strID)
- {
- register Handle tempH;
- UInt32 theCount = 0UL;
-
- tempH = Get1Resource('ULNG', strID);
- if (tempH) {
- theCount = *(UInt32 *)*tempH;
- ReleaseResource(tempH);
- }
- return theCount;
- }
-
- Boolean SetupBooleanFromPrefs(short strID)
- {
- register Handle tempH;
- Boolean theCount = false;
-
- tempH = Get1Resource('BOOL', strID);
- if (tempH) {
- theCount = *(Boolean *)*tempH;
- ReleaseResource(tempH);
- }
- return theCount;
- }
-
- Boolean FinderIsFront(void)
- {
- long response;
- ProcessSerialNumber process;
- ProcessInfoRec info;
- Boolean front = false;
-
- if (Gestalt(gestaltOSAttr, &response) == noErr && (response & (1L << gestaltLaunchControl))) {
- if (GetFrontProcess(&process) == noErr) {
- info.processInfoLength = sizeof(ProcessInfoRec);
- info.processName = 0L;
- info.processAppSpec = 0L;
- if (GetProcessInformation(&process, &info) == noErr && info.processSignature == 'MACS')
- front = true;
- }
- }
- return front;
- }
-
- #pragma segment CleanUp
-
- void AddString(StringHandle theAddedStr, short strID)
- {
- register Handle tempH;
-
- SetResLoad(false);
- tempH = Get1Resource('STR ', strID);
- SetResLoad(true);
- if (tempH) {
- RemoveResource(tempH);
- DisposeHandle(tempH);
- }
- if (theAddedStr)
- AddResource((Handle)theAddedStr, 'STR ', strID, "\p");
- }
-
- void AddULong(UInt32 theAddedL, short strID)
- {
- register Handle tempH;
-
- SetResLoad(false);
- tempH = Get1Resource('ULNG', strID);
- SetResLoad(true);
- if (tempH) {
- RemoveResource(tempH);
- DisposeHandle(tempH);
- }
- tempH = NewHandle(sizeof(UInt32));
- if (tempH) {
- *(UInt32 *)*tempH = theAddedL;
- AddResource(tempH, 'ULNG', strID, "\p");
- }
- }
-
- void AddBoolean(Boolean theAddedL, short strID)
- {
- register Handle tempH;
-
- SetResLoad(false);
- tempH = Get1Resource('BOOL', strID);
- SetResLoad(true);
- if (tempH) {
- RemoveResource(tempH);
- DisposeHandle(tempH);
- }
- tempH = NewHandle(sizeof(Boolean));
- if (tempH) {
- *(Boolean *)*tempH = theAddedL;
- AddResource(tempH, 'BOOL', strID, "\p");
- }
- }
-
- // check out the last #pragma segment !!!!!
-